//
//  RJDatePickerTextField.m
//  Campus
//
//  Created by hzyd-wlj on 14-9-11.
//  Copyright (c) 2014年 hzyd. All rights reserved.
//

#import "RJDatePickerTextField.h"
#import "NSDate+Helper.h"

@implementation RJDatePickerTextField


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupDatePicker];
    }
    return self;
}

#pragma mark - Nib/Storyboard Initializers
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Setup the view defaults
        [self setupDatePicker];
    }
    return self;
}

#pragma mark - Setups
- (void)setupDatePicker
{
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    self.inputView = _datePicker;
}

#pragma mark - DatePicker Changed Value Method
- (void)dateChanged:(UIDatePicker *)picker
{
    [self changeText];
}

- (void)changeText
{
    if (self.datePicker.datePickerMode == UIDatePickerModeDate) {
        self.text = [self.datePicker.date stringWithFormat:@"yyyy-MM-dd"];
    }else if(self.datePicker.datePickerMode == UIDatePickerModeTime){
        self.text = [self.datePicker.date stringWithFormat:@"HH:mm"];
    }else{
        self.text = [self.datePicker.date stringWithFormat:@"yyyy-MM-dd HH:mm"];
    }
}

- (void)textFieldDidBeginEditing:(NSNotification *)notification
{
//    [super textFieldDidBeginEditing:notification];
    [self changeText];
}
@end
